//
//  CZCommentCell.swift
//  GZWeibo666
//
//  Created by Yuan on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZCommentCell: UITableViewCell {
    
    var comment : CZComment? {
        didSet{
            // 设置头像头像
            if let profile_image_url = comment?.user?.profile_image_url{
                // 使用SDWebImage下载头像
                iconView.sd_setImageWithURL(NSURL(string: profile_image_url), placeholderImage: UIImage(named: "avatar"))
            }
            
            // 设置用户名称
            // = 右边可以使用?是因为nameLabel.text是一个可选
            nameView.text = comment?.user?.screen_name
            
            // 时间
            sourceView.text = comment?.created_at
            //内容
            contantText.text = comment?.text
            
            // 设置认证图标,根据不同的数字来设置不同的图片
            verifiedImageView.image = comment?.user?.verifiedImage
            
            
            
            // 会员等级
            
            mbrankImageView.image = comment?.user?.mbrankImage
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: -  计算行高
    func rowHeight(comment: CZComment) -> CGFloat {
        // 根据模型来设置内容, 当我们把传入进来的模型赋值给self.status cell里面的内容被设置成为status的内容
        self.comment = comment
        
        // 根据新的内容更新约束
        self.layoutIfNeeded()
        
        return CGRectGetMaxY(contantText.frame)+10
    }
    //MARK: - 准备控件
    func prepareUI() {
        addSubview(iconView)
        addSubview(nameView)
        addSubview(sourceView)
        addSubview(contantText)
        addSubview(mbrankImageView)
        addSubview(verifiedImageView)
        
        /// 用户头像,在topView底部,左下有间距
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: 35, height: 35), offset: CGPoint(x: CZStatusCellMargin, y: CZStatusCellMargin))
        
        /// 用户名称
        nameView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 时间
        sourceView.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        //内容
        contantText.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: sourceView, size: nil, offset: CGPoint(x: 0, y: 10))
        /// 会员图标
        mbrankImageView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameView, size: CGSize(width: 14, height: 14), offset: CGPoint(x: CZStatusCellMargin, y: 0))
        /// 认证图标
        verifiedImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
    }
    //头像
    private lazy var iconView : UIImageView = UIImageView()
    //名称
    private lazy var nameView : UILabel = UILabel(color: UIColor.blackColor(), fontSize: 13)
    //创建时间
    private lazy var sourceView : UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 10)
    //内容
    private lazy var contantText : UILabel = {
        let tex = UILabel(color: UIColor.blackColor(), fontSize: 13)
        tex.numberOfLines = 0
        tex.sizeToFit()
        return tex
    }()
    /// 会员等级图标
    private lazy var mbrankImageView: UIImageView = UIImageView()
    
    /// 认证类型图标
    private lazy var verifiedImageView:  UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
}
